<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <form class="layui-form" action="" lay-filter="formExample">
            <div class="layui-form-item">
                <label class="layui-form-label"><span style="color: red;">*</span>权限组名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" lay-verify="required"  autocomplete="off" class="layui-input" id="nameNow">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline">
                    <input type="text" name="remark"  autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux"></div>
            </div>

            <div class="layui-form-item" hidden>
                <label class="layui-form-label">权限组ID</label>
                <div class="layui-input-inline">
                    <input type="text" name="idNow" id='idNow' class="layui-input">
                </div>
            </div>

            <div class="layui-form-item" hidden>
                <label class="layui-form-label">是否更改权限</label>
                <div class="layui-input-inline">
                    <input type="text" name="changeRole" id='changeRole' class="layui-input">
                </div>
            </div>

            <div id="view"></div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="demo1">确定</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/html" id="demo">
    <div class="layui-form-item">
        <label class="layui-form-label">拥有权限</label>
        <div class="layui-input-inline">
            <input type="text" name="dataAuthority" placeholder="点击查看" readonly="readonly" autocomplete="off" class="layui-input" id="dataAuthority" value="">
        </div>
    </div>
</script>

<script>
    layui.use(['form', 'layedit', 'laydate','tree','tableSelect','laytpl'], function () {
        var form = layui.form,
            layer = layui.layer,
            laytpl = layui.laytpl

        $('#changeRole').val('no');

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        /**
         *模板渲染可能不会显示的"数据权限"模块
         */
        var data = data_authority;
        if (data == undefined) {
            data = []
        }
        var getTpl = demo.innerHTML
            , view = document.getElementById('view');
        laytpl(getTpl).render(data, function (html) {
            view.innerHTML = html;
        });

        //表单初始赋值
        /*data_authority是从groupAuth页面传递过来的*/
        if (data_authority) {
            //id输入框不可用
            $('#nameNow').val(data_authority.name);
            $('#idNow').val(data_authority.id);

            //赋值
            form.val('formExample', {
                "name": data_authority.name,
                "id":data_authority.id,
                "remark":data_authority.remark,
                "create_by":"",
                "dataAuthority":"",
                "changeRole":$('#changeRole').val()
            })
        }

        //确定
        form.on('submit(demo1)', function (data) {
            debugger
            if (data_authority){
                data.field["type"] = "edit"
            }else{
                data.field["type"] = "add"
            }

            if(data.field["changeRole"] == 'yes'){
                data.field["dataAuthority"] = checkedData;
            }

            debugger
            shared.ajaxPost('/sys-mgr/addOrEditAuthority',{'dataObj': data.field}, function (res) {
                debugger
                if (res) {
                    layer.msg(res, {
                        icon: 1,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    });
                }
                debugger
                layer.closeAll();
            })
            return false;
        })

        /*点击数据权限,触发弹框,进行数据权限选择*/
        $("#dataAuthority").on("click", function () {
            $('#changeRole').val('yes');
            var content = layui.miniPage.getHrefContent('mypages/usual/systemMgt/chooseAuthority.html');
            var openWH = layui.miniPage.getOpenWidthHeight();
            //不能用 var 前缀
            opreatType = "add";
            var index = layer.open({
                title: '选择权限',
                type: 1,
                shade: 0.2,
                maxmin: true,
                zIndex: 19891020,
                shadeClose: true,
                offset: '200px',
                area : [ '300px', '500px' ],
                content: content,
                end: function () {
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        });
    })
</script>